package com.rcp.server;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rcp.objects.Order;
import com.rcp.objects.Side;

@WebServlet("/Remove")
public class Remove extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public Remove() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		List sides = new ArrayList();
		HttpSession session = request.getSession();
		ArrayList<Order> totalOrder = (ArrayList<Order>) session.getAttribute("totalOrder");
		
		String url = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
        String username = "cs437group01";
        String password = "VHj#dj!v";
        
//connecting to database		
		Connection c;
		try {
			c = DriverManager
			    .getConnection( url, username, password );
			
			Statement stmt = c.createStatement();
			
		ResultSet rs = stmt.executeQuery("SELECT * FROM  Entrees WHERE Entrees.Name =  '" + 
				request.getParameter("entree") +"'");
		
		while( rs.next() )
        {	            		
			totalOrder.add(new Order(rs.getString("Name"), rs.getFloat("cost")) );
        }
//sides
		rs = stmt.executeQuery("SELECT * FROM  Sides");
		
		while( rs.next() )
        {	            		
			sides.add(new Side(rs.getString("Name")));
        }
		
		getServletContext().setAttribute( "sides", sides );
    	getServletContext().setAttribute( "totalOrder", totalOrder );
        request.getRequestDispatcher("/WEB-INF/server/Remove.jsp").forward(request, response);
//        ArrayList<Order> totalOrder = (ArrayList<Order>) getServletContext().getAttribute("totalOrder");
//        ArrayList<String> ingredients = new ArrayList<String>();
//        
////getting entree id
//        
//        System.out.println("starting ramove.java");
//		String[] temp = request.getParameterValues("entree");
//		String entree = "";
//		for(int i = 0; i < temp.length; i++){
//			entree = entree + " " + temp[i];
//		}
//		entree = entree.substring(1);
//
////adding order to array
//		Order newOrder = new Order(entree);
//			
//		String[] mealSides = request.getParameterValues("sides");
//		for(int i=0; i < mealSides.length; i++){
//			newOrder.setSides(new Side(mealSides[i]));
//		}
//		totalOrder.add(newOrder);
//		

//		
//		System.out.println(entreeID);
//		//ResultSet rs = null;
//		
//		rs = stmt.executeQuery("SELECT Ingredients.Name " +
//				"FROM Ingredients " +
//				"LEFT JOIN Recipes " + 
//				"ON Ingredients.IngredientID = Recipes.IngredientID " +
//				"WHERE Recipes.EntreeID = '" + entreeID +"'");
//		
//		while( rs.next() )
//        {	            		
//			ingredients.add(rs.getString("Name"));
//        }
//		System.out.println(newOrder.getEntree() + "----------------------------");
//		getServletContext().setAttribute( " currentOrder", newOrder.getEntree());
//		getServletContext().setAttribute( "ingredients", ingredients );
//		request.getRequestDispatcher("Remove.jsp").forward(request, response);
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
